package net.bxx2004.pandalib.bukkit.block;

import com.alibaba.fastjson.JSONObject;
import net.bxx2004.pandalib.PandaLib;
import net.bxx2004.pandalib.bukkit.file.JSONData;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;

import java.io.File;

public class BlockMeta {
    public static final String data = PandaLib.PATH().getAbsolutePath()+ "/blockdata";
    private Block block;
    private JSONData odata;
    public BlockMeta(Block block){
        this.block = block;
        update();
    }
    public void init(){
        odata.create(block.getX() + "," + block.getY() + "," + block.getZ(),"{}");
    }
    public void clear(){
        odata.delete(block.getX() + "," + block.getY() + "," + block.getZ());
    }
    public boolean hasDataFile(){
        return odata.data(block.getX() + "," + block.getY() + "," + block.getZ()) != null;
    }
    public JSONObject get(){
        return odata.data(block.getX() + "," + block.getY() + "," + block.getZ());
    }
    public void set(JSONObject object){
        odata.set(block.getX() + "," + block.getY() + "," + block.getZ(),object);
    }
    private void update(){
        if (!new File(data).exists()){
            new File(data).mkdirs();
        }
        File chunkdir = new File(data + "/" + block.getWorld().getName() + "," + block.getX() + "," + block.getZ());
        if (!chunkdir.exists()){
            chunkdir.mkdirs();
        }
        JSONData data = new JSONData(chunkdir);
        odata = data;
    }
    public static BlockMeta of(String world,int x,int y,int z){
        return new BlockMeta(Bukkit.getWorld(world).getBlockAt(x,y,z));
    }
    public static BlockMeta of(int x,int y,int z){
        return new BlockMeta(Bukkit.getWorld("world").getBlockAt(x,y,z));
    }
    public static Block block(String world,int x,int y,int z){
        return Bukkit.getWorld(world).getBlockAt(x,y,z);
    }
}
